<template>
  <div>
    <el-card>
      <el-row>
        <el-col align="right">
          <el-form :inline="true" class="demo-form-inline">
            <el-form-item>
              <el-input
                  v-model="searchvalue"
                  clearable
                  @input="call"
                  placeholder="请输入回访信息"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button v-permission="['Admin','Sadmin']"
                         @click="getList"
                         icon="el-icon-search"
                         type="primary">
                查询
              </el-button>
            </el-form-item>
            <el-form-item>
              <el-button v-permission="['Admin','Sadmin']"
                         @click="dialogFormVisible = true;isbianji = true"
                         icon="el-icon-circle-plus-outline"
                         type="success">
                新增回访
              </el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-table
          border
          :data="list"
          style="width: 100%">
        <el-table-column
            prop="detail"
            label="回访细节"
            align="center">
        </el-table-column>
        <el-table-column
            prop="employeeName"
            align="center"
            label="员工姓名">
        </el-table-column>
        <el-table-column
            prop="visitorName"
            align="center"
            label="回访客户姓名">
        </el-table-column>
        <el-table-column
            prop="recallMv"
            label="回访的音频和视频"
            align="center">
        </el-table-column>
        <el-table-column
            prop="recallPic"
            align="center"
            label="回访信息中的图片">
        </el-table-column>
        <el-table-column
            prop="time"
            align="center"
            label="录入时间">
        </el-table-column>
        <el-table-column
            align="center"
            label="操作">
          <template slot-scope="{ row }">
            <el-button
                v-permission="['Admin','Sadmin','Saler','Salemanager']"
                @click="bianji(row)"
                type="primary"
                size="mini">
              编辑
            </el-button>
            <el-button
                v-permission="['Admin','Sadmin','Salemanager']"
                @click="delhuifang(row.id)"
                type="danger"
                size="mini">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-col align="right">
        <el-row style="margin:15px">
          <el-pagination
              background
              @current-change="currentChange"
              :page-size="limit"
              layout="total,  prev, pager, next, jumper"
              :total="total">
          </el-pagination>

          <el-button type="primary" @click="export2Excel()">导出Excel</el-button>
        </el-row>
      </el-col>
    </el-card>
    <el-dialog @close="close"
               :visible.sync="dialogFormVisible">
      <el-form ref="form"
               :model="form"
               :rules="rules">
        <el-form-item label="回访细节" label-width="100px" prop="detail">
          <el-input v-model="form.detail" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="回访时间" label-width="100px" prop="time">
          <el-input v-model="form.time" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addhuifang, delhuifang, huifang, modifyhuifang} from "../../api";
import {deepClone, xx} from "../../uilts";

const getform = () => ({
  detail: '',
  time: ''
})
export default {
  name: "huifang",

  data() {
    return {
      list: [],
      searchvalue: '',
      dialogFormVisible: false,
      isbianji: false,//编辑
      page: 1, //当前页码
      total: 0,//总条数
      form: getform(),
      rules: {
        detail: xx(),
        time: xx()
      },
      limit: 6,
      tableData: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    export2Excel() {
      require.ensure([], () => {
        const {export_json_to_excel} = require("@/excel/Export2Excel");
        const fieldName = ["索引", "客户名", "回访时间","回访人","回访附件"];
        const filterVal = ["id", "visitorName", "time","employeeName","recallPic"];
        const data = this.list.map(v => filterVal.map(j => v[j]));
        export_json_to_excel(fieldName, data, "用户列表");
      })
    },
    async getList() {
      const res = await huifang({
        limit: 30,
        page: this.page
      })
      this.list = res.data.data.list
      this.page = res.data.data.pageNum
      this.total = res.data.data.total
      console.log(res.data.data.list)
    },
    //查询
    call() {
      if (this.searchvalue === '') {
        this.getList()
      }
    },
    //翻页
    currentChange(pagee) {
      this.page = pagee
      this.getList()
    },
    //新增and修改
    bianji(row) {
      this.isbianji = false
      this.dialogFormVisible = true
      this.form = deepClone(row)
    },
    submit() {
      this.$refs.form.validate(async (res) => {
        if (res) {
          if (this.isbianji) {
            await addhuifang(this.form)
          } else {
            await modifyhuifang(this.form)
          }
          this.$message.success('操作成功！')
          this.dialogFormVisible = false
          await this.getList()
        }
      })
    },
    //删除
    delhuifang(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await delhuifang(id)
        this.$message.success('删除成功！')
        await this.getList()

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    close() {
      this.form = getform()
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style scoped>

</style>